#include "AppStateMenu.h"


void CAppStateMenu::OnKeyOptions(SDLKey sym)
{
	switch(sym) {

		case SDLK_UP:
		{
			CSoundBank::SoundControl.Play(0, 1);
			choice--;
			if(choice<0)
				choice = 3;
				
			break;
		}
		case SDLK_DOWN:
		{
			CSoundBank::SoundControl.Play(0, 1);
			this->choice++;
			if(this->choice>3)
				this->choice = 0;
				
			break;
		}
		case SDLK_s:
		{
			CSoundBank::SoundControl.Play(0, 1);
			this->choice++;
			if(this->choice>3)
				this->choice = 0;
				
			break;
		}
		case SDLK_w:
		{
			CSoundBank::SoundControl.Play(0, 1);
			choice--;
			if(choice<0)
				choice = 3;
				
			break;
		}
		case SDLK_ESCAPE:
		{
			mainChoice = MAINMENU_CHOICE;
			choice = 1;
			break;
		}
		case SDLK_RIGHT: //Adjust volume(+5)
		{
			int  CurrentVolume = CSoundBank::SoundControl.GetVolume(0);
			if(choice == 1 && CSoundBank::SoundControl.GetVolume(0) < 123)
				CSoundBank::SoundControl.SetVolume(CurrentVolume+5, 0);
			else if(choice == 1)
				CSoundBank::SoundControl.SetVolume(128, 0);
			break;
		}
		case SDLK_d: //Adjust volume(+5)
		{
			int  CurrentVolume = CSoundBank::SoundControl.GetVolume(0);
			if(choice == 1 && CSoundBank::SoundControl.GetVolume(0) < 123)
				CSoundBank::SoundControl.SetVolume(CurrentVolume+5, 0);
			else if(choice == 1)
				CSoundBank::SoundControl.SetVolume(128, 0);
			break;
		}
		case SDLK_a: //Adjust volume(-5) 
		{
			int  CurrentVolume = CSoundBank::SoundControl.GetVolume(0);
			if(choice == 1 && CSoundBank::SoundControl.GetVolume(0) > 4)
				CSoundBank::SoundControl.SetVolume(CurrentVolume-5, 0);
			else if(choice == 1)
				CSoundBank::SoundControl.SetVolume(0, 0);
			break;
		}
		case SDLK_LEFT: //Adjust volume(-5) 
		{
			int  CurrentVolume = CSoundBank::SoundControl.GetVolume(0);
			if(choice == 1 && CSoundBank::SoundControl.GetVolume(0) > 4)
				CSoundBank::SoundControl.SetVolume(CurrentVolume-5, 0);
			else if(choice == 1)
				CSoundBank::SoundControl.SetVolume(0, 0);
			break;
		}
		case SDLK_RETURN:
		{
			switch(choice)
			{
			case 0: //Set fullscreen on/off
				{
					screenChoice = true;
					if(fullscreen)
						fullscreen = false;
					else 
						fullscreen = true;
					break;
				}
			case 1: //Adjust volume left(--) right(++) enter(0/maxVolume)
				{
					if(CSoundBank::SoundControl.GetVolume(0) == 0)
						CSoundBank::SoundControl.SetVolume(128, 0);
					else
						CSoundBank::SoundControl.SetVolume(0, 0);
					break;
				}
			case 2://Control options
				{
					mainChoice = CONTROL_CHOICE;
					choice = 0;
					break;
				}
			case 3: //return to main Screen
				{
					mainChoice = MAINMENU_CHOICE;
					choice = 1;
					break;
				}
			}
		}
	}
}

//Render mainMenu
void CAppStateMenu::OnRenderOptions(SDL_Surface* Surf_Display)
{
	//surface for drawing text
	SDL_Surface* Text_Solid;

	SDL_Colour black = {0,0,0};

	//rect for placing text and box. 
	SDL_Rect Rect;
	Rect.x = 150;
	Rect.y = 150;
		
	//draw box
	SDL_BlitSurface(OptionBox, NULL, Surf_Display, &Rect);

	//choice dot
	SDL_Rect menuRect;
	menuRect.h = 10;
	menuRect.w = 10;
	menuRect.x = 180;
	menuRect.y = 190 + 40 * choice;

	//skip position if y = 270
	if(menuRect.y == 270)//skip volume slider position
		menuRect.y = 310;
	else if(menuRect.y == 310)
		menuRect.y = 350;

	SDL_FillRect(Surf_Display, &menuRect, 0);

	//draw choiceText
	std::string Text[3] = {"< Audio > ", "Controls>", "Return "};
	Rect.x = 200;
	for(int i = 0; i < 4; i++)
	{
		Rect.y = 170 + 40 * i;

		if(Rect.y == 250)//skip volume slider position
			Rect.y = 290;

		else if(Rect.y == 290)
			Rect.y = 330;

		if(i == 0)
			Text_Solid = TTF_RenderText_Blended(menuChoiceFont, optionsText.c_str(), black);

		else if(i <= 3)
			Text_Solid = TTF_RenderText_Blended(menuChoiceFont, Text[i-1].c_str(), black);

		SDL_BlitSurface(Text_Solid, NULL, Surf_Display, &Rect);
		SDL_FreeSurface(Text_Solid);
	}

	//Draw VolumeSlider
	Rect.y = 260;
	Rect.h = 20;
	Rect.w = CSoundBank::SoundControl.GetVolume(0);
	SDL_FillRect(Surf_Display, &Rect, 0);

	//Set video mode to fullscreen or windowed.
	if(fullscreen && screenChoice)
	{
		Surf_Display = SDL_SetVideoMode(WWIDTH, WHEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
		screenChoice = false;
		optionsText = "FullScreen on";
	}
	else if(!fullscreen && screenChoice)
	{
		Surf_Display = SDL_SetVideoMode(WWIDTH, WHEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
		screenChoice = false;
		optionsText = "FullScreen off";
	}
}